package cn.wildfire.chat.app.main;

import android.view.MotionEvent;

import androidx.lifecycle.ViewModelProviders;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.ui.SendUserCardWindow;
import cn.wildfire.chat.app.extendsion.ui.SmoothToggleAndSlideAnimationForUI;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.contact.ContactViewModel;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.pick.CheckableUserListAdapter;
import cn.wildfire.chat.kit.contact.pick.PickUserViewModel;
import cn.wildfire.chat.kit.group.BasePickGroupMemberActivity;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * Create by HEKE, 2019/9/23 18:03
 * TODO 选择聊天页面
 */
public class ChooseUsercardReceiverActivity extends BasePickGroupMemberActivity {

    private List<UIUserInfo> checkedReceiverMembers;
    // 显示总数据
    private List<UIUserInfo> checkList;
    private ContactViewModel contactViewModel;
    private boolean onCreate;

    protected UserInfo friendUserInfo;
    protected UserInfo selfUserInfo;

    @Override
    protected void onGroupMemberChecked(List<UIUserInfo> checkedUserInfos) {
        this.checkedReceiverMembers = checkedUserInfos;
        /**
         * 弹窗发送名片
         * */
        try {
            if (this.checkedReceiverMembers.size() > 0) {
                UserInfo targetUserInfo = this.checkedReceiverMembers.get(this.checkedReceiverMembers.size() - 1).getUserInfo();
                SendUserCardWindow sendUserCardWindow = new SendUserCardWindow(ChooseUsercardReceiverActivity.this,
                        friendUserInfo, targetUserInfo, selfUserInfo, new SendUserCardWindow.OnSenduserCardWindowListener() {
                    @Override
                    public void cancle() {
                        try {
                            // 释放所有选中项
                            CheckableUserListAdapter checkableUserListAdapter = (CheckableUserListAdapter) pickGroupMemberFragment.getUsersRecyclerView().getAdapter();
                            List<UIUserInfo> userInfoList = pickUserViewModel.getUsers();
                            for (int i = 0; i < userInfoList.size(); i++) {
                                UIUserInfo uiUserInfo = userInfoList.get(i);
                                uiUserInfo.setChecked(false);
                                if (checkableUserListAdapter != null) {
                                    checkableUserListAdapter.notifyItemChanged(i);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                sendUserCardWindow.show(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int menu() {
        return 0;
    }

    /**
     * modify by HEKE , 2019/9/4 18:55 TODO 测试滑动隐藏或显示搜索栏 HEKE_SEARCHLAYOUT_ADDMANAGER
     */
    protected SmoothToggleAndSlideAnimationForUI smoothToggleAndSlideAnimationForUI;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            if (smoothToggleAndSlideAnimationForUI == null) {
                smoothToggleAndSlideAnimationForUI = new SmoothToggleAndSlideAnimationForUI(this,
                        SmoothToggleAndSlideAnimationForUI.FLAG_GROUP_ADD_REMOVE_MANMAGER);
                smoothToggleAndSlideAnimationForUI.setToggleView(pickGroupMemberFragment.getSearchLayout());
                smoothToggleAndSlideAnimationForUI.setSlideView(pickGroupMemberFragment.getUsersRecyclerView(), 0);
            }
            smoothToggleAndSlideAnimationForUI.onTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void beforeViews() {
        super.beforeViews();
        onCreate = true;
        pickUserViewModel = ViewModelProviders.of(this).get(PickUserViewModel.class);
        friendUserInfo = getIntent().getParcelableExtra("friendUserInfo");
        selfUserInfo = ChatManager.Instance().getUserInfo(ChatManager.Instance().getUserId(), false);
    }

    @Override
    protected void afterViews() {
        isNotGroupChoose = true;
        ownerId = selfUserInfo.uid;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.choose_usercard_reveiver_title);
        setSearchLayoutVisibility(false);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        try {
            /**
             * 发送名片只能给一人
             * */
            if (hasFocus && onCreate) {
                onCreate = false;
                pickUserViewModel.setInitialCheckedIds(new ArrayList<>());
                bindListener();
                contactViewModel = WfcUIKit.getAppScopeViewModel(ContactViewModel.class);
                List<UserInfo> userInfoList = contactViewModel.getContacts(false);
                List<UserInfo> tempCheckList = new ArrayList<>();
                for (int i = 0; i < userInfoList.size(); i++) {
                    UserInfo userInfo = userInfoList.get(i);
                    if (!(userInfo.uid.equals(selfUserInfo.uid) || userInfo.uid.equals(friendUserInfo.uid))) {
                        tempCheckList.add(userInfo);
                    }
                }
                checkList = UIUserInfo.userInfoToUIUserInfo(tempCheckList);
                pickUserViewModel.setUsers(checkList);
                initView();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (pickGroupMemberFragment != null) {
                            pickGroupMemberFragment.showQuickIndexBar(true);
                        }
                    }
                }, 500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
